// ignore_for_file: no_leading_underscores_for_local_identifiers

import 'package:flutter/cupertino.dart';
import 'package:flutter/src/widgets/container.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:lz_app_core/constants/app_manager.dart';
import 'package:lz_app_core/enum/app_enum.dart';
import 'package:lz_base/kd_base.dart';
import 'package:lz_form/kd_form.dart';
import 'package:lz_login/kd_login.dart';
import 'package:lz_login/src/serve/kd_login_api.dart';
import 'package:lz_login/src/serve/models/kd_mine_certification_info_model.dart';
import 'package:lz_router/kd_router.dart';
import 'package:lz_ui/kd_ui.dart';
import 'package:ec_adapter/ec_adapter.dart';

 
class KDMineInfoPage extends KDFormBasePage with KDRouterObserverType {
  final KDMineInfoPageVM _pageVM = KDMineInfoPageVM();

  KDMineInfoPage({super.key});

  @override
  setupElement(StatelessElement element) {
    KDRouter.addObserverPage(this);
    _pageVM.requestData();
  }

  /// override
  @override
  String? get title => "我的信息";

  /// 实现vm的传递
  @override
  KDFormBasePageVM get pageVM => _pageVM;

  // @override
  // double get positionTopHeight => isOwner ? KD.size(96) : 0.0;

  // @override
  // EdgeInsets? get kdPadding => EdgeInsets.symmetric(horizontal: KD.size(32));

  // @override
  // Widget? get positionTopWidget {
  //   if (isOwner) {
  //     return ECSegmentWidget(
  //       const ["基础信息", "开票信息"],
  //       height: KD.size(96),
  //       didSelectIndex: (index) {
  //         if (index == 0) {
  //           _pageVM.requestData();
  //         } else {
  //           _pageVM.reloadKP();
  //         }
  //       },
  //     );
  //   }
  //   return null;
  // }

  bool get isOwner => AppManager.appName == AppName.kdOwner;

  @override
  String get routerName => KDLoginRouter.mineInfo;

  @override
  viewWillAppear() {
    _pageVM.requestData();
  }
}

class KDMineInfoPageVM extends KDFormBasePageVM {
  List<ListViewItemType> inofList = [];

  requestData() async {
    List<KDMineCertificationInfoModel>? models = await KDLoginApi.mineInfo();

    if (models != null) {
      inofList.clear();

      KDMineCertificationInfoModel? info1;
      KDMineCertificationInfoModel? info2;

      // 把实名认证和法人实名认证过滤出来
      for (var model in models) {
        if (model.authType != null && model.authType == "01") {
          info1 = model;
        }
        if (model.authType != null && model.authType == "06") {
          info2 = model;
        }

        for (var model1 in models) {
          if (model1.authType != null && model1.authType == "02") {
            model1.realAuthStatus = info1?.authStatus;
          }
          if (model1.authType != null && model1.authType == "07") {
            model1.farenRealAuthStatus = info2?.authStatus;
          }
        }
      }

      // 模型构建
      for (var model in models) {
        KDCertificationType? type = KDCertificationType.from(model.authType);
        if (type == null ||
            type == KDCertificationType.realNameAuthentication) {
          continue;
        }
        List<ListViewItemType> list = type.certificationVmList(model);

        inofList.addAll(list);
      }
    }
    datas.clear();
    datas.addAll(inofList);
    update();
  }

  // /// 加载开票信息
  // reloadKP() {
  //   datas.clear();
  //   datas.addAll(kpList);
  //   update();
  // }
}

extension KDCertificationTypeMineInfoExtension on KDCertificationType {
  _toRouter(KDMineCertificationInfoModel model) {
    if (model.getAuthType == null) return;

    eventBus.fire(KDCertificationEvntBusModel.look(
        authStatus: model.stateType,
        authType: model.getAuthType!,
        memberAuthId: model.memberAuthId));
  }

  List<ListViewItemType> certificationVmList(
      KDMineCertificationInfoModel model) {
    KDCertificationStateType stateType = model.stateType;
    void _lockCertification() {
      // 权限
      if (KDUserPermission.checkType(
          KDUserPermissionType.mine_certificate_information)) _toRouter(model);
    }

    String? endTime =
        (model.isEverEnable ?? "") == "1" ? "长期" : model.certificateEndDate;
    switch (this) {
      case KDCertificationType.realNameAuthentication:
        return [];
      case KDCertificationType.corporateIdentityCard:
      case KDCertificationType.identity:
        String statu = this == KDCertificationType.identity
            ? (model.realAuthStatus ?? "0")
            : (model.farenRealAuthStatus ?? "0");
        KDCertificationStateType realType =
            KDCertificationStateType.from(int.parse(statu));

        return [
          KDFormSectionTitleWidgetVM(title: "$headerName信息"),
          KDFormTitleWidgetVM(
              title: "姓名", isBLine: true, subTitle: model.certificateUserName),
          KDFormTitleWidgetVM(
              title: "证件号", isBLine: true, subTitle: model.certificateNumber),
          KDFormTitleWidgetVM(
            title: "实名认证",
            subIcon: realType.getTypeIcon(),
            isBLine: true,
          ),
          KDFormTitleWidgetVM(
            title: "身份证照片",
            isBLine: true,
            showArrowRight: true,
            subIcon: stateType.getTypeIcon(),
            didClick: _lockCertification,
          ),
          KDFormTitleWidgetVM(
              title: "身份证有效期",
              isBLine: true,
              subTitle: model.certificateEndDate),
        ];
      case KDCertificationType.driverLicense:
        return [
          KDFormSectionTitleWidgetVM(title: "$headerName信息"),
          KDFormTitleWidgetVM(
              title: "姓名", isBLine: true, subTitle: model.certificateUserName),
          KDFormTitleWidgetVM(
              title: "驾驶证编号", isBLine: true, subTitle: model.certificateNumber),
          KDFormTitleWidgetVM(
              title: "准驾车型", isBLine: true, subTitle: model.driverLicenseType),
          KDFormTitleWidgetVM(
              title: "发证机关",
              isBLine: true,
              subTitle: model.licenseIssuingAuthority),
          KDFormTitleWidgetVM(
              title: "发证日期",
              isBLine: true,
              subTitle: model.certificateStartDate),
          KDFormTitleWidgetVM(title: "到期日期", isBLine: true, subTitle: endTime),
          KDFormTitleWidgetVM(
              title: "驾驶证照片",
              didClick: _lockCertification,
              subIcon: stateType.getTypeIcon(),
              isBLine: true,
              showArrowRight: true),
        ];
      case KDCertificationType.employed:
        return [
          KDFormSectionTitleWidgetVM(title: "从业资格信息"),
          // KDFormTitleWidgetVM(
          //     title: "从业资格证",
          //     isBLine: true,
          //     subTitle: model.certificateNumber),
          // KDFormTitleWidgetVM(
          //     title: "营业期限结束日期", isBLine: true, subTitle: endTime),
          KDFormTitleWidgetVM(
            title: "从业资格图片",
            isBLine: true,
            showArrowRight: true,
            didClick: _lockCertification,
            subIcon: stateType.getTypeIcon(),
          ),
        ];
      case KDCertificationType.roadLicense:
        return [
          KDFormSectionTitleWidgetVM(title: headerName),
          KDFormTitleWidgetVM(
              title: "业户名称",
              isBLine: true,
              subTitle: model.certificateUserName),
          KDFormTitleWidgetVM(
              title: "经营许可证号",
              isBLine: true,
              subTitle: model.certificateNumber),
          KDFormTitleWidgetVM(
              title: "发证日期",
              isBLine: true,
              subTitle: model.certificateStartDate),
          KDFormTitleWidgetVM(title: "到期日期", isBLine: true, subTitle: endTime),
          KDFormTitleWidgetVM(
            title: "道路运输经营许可证照片",
            isBLine: true,
            showArrowRight: true,
            didClick: _lockCertification,
            subIcon: stateType.getTypeIcon(),
          ),
        ];
      case KDCertificationType.businessLicense:
        return [
          KDFormSectionTitleWidgetVM(title: "企业信息"),
          KDFormTitleWidgetVM(
              title: "企业名称", isBLine: true, subTitle: model.companyName),
          KDFormTitleWidgetVM(
              title: "注册地址", isBLine: true, subTitle: model.residence),
          KDFormTitleWidgetVM(
              title: "统一社会信用代码",
              isBLine: true,
              subTitle: model.certificateNumber),
          KDFormTitleWidgetVM(
              title: "营业期限开始日期",
              isBLine: true,
              subTitle: model.certificateStartDate),
          KDFormTitleWidgetVM(
              title: "营业期限结束日期", isBLine: true, subTitle: endTime),
          KDFormTitleWidgetVM(
            title: "营业执照照片",
            isBLine: true,
            showArrowRight: true,
            subIcon: stateType.getTypeIcon(),
            didClick: _lockCertification,
          ),
        ];
    }
  }
}
